package com.yunhe.authority.controller.authority;

import com.github.stuxuhai.jpinyin.PinyinException;
import com.yunhe.authority.domain.authority.RealStation;
import com.yunhe.authority.model.RealStationOfficeVo;
import com.yunhe.authority.model.RealStationVo;
import com.yunhe.authority.model.inspect.RealStationOfficeCompanyVo;
import com.yunhe.authority.service.authority.RealStationService;
import com.yunhe.common.annotation.OperationRecord;
import com.yunhe.common.constant.SystemConsts;
import com.yunhe.common.exception.ObjectNotFoundException;
import com.yunhe.common.exception.UniqueConstraintsException;
import com.yunhe.common.filter.ServerContextHolder;
import com.yunhe.common.model.ResultObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * @author zhengbo
 * @date 2021/5/11 16:44
 */
@Api(value = "realStation",description = "真实园区", tags = {"Authority_RealStation"})
@RestController
@RequestMapping("/realStation")
public class RealStationController {

    @Autowired
    private RealStationService realStationService;

    /**
     * 新增真实园区
     * @param realStation 电站对象
     */
    @ApiOperation("新增真实园区")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "stationId", value = "总园区ID", required = true, dataType = "long", paramType = "query")
    })
    @RequestMapping(value = "/creatRealStation", method = RequestMethod.POST)
    @OperationRecord(menu = "真实园区管理", actionType = SystemConsts.ACTION.ADD)
    public ResponseEntity<ResultObject> post(@RequestBody RealStation realStation,
                                             @RequestParam("stationId") Long stationId) throws PinyinException, UniqueConstraintsException {
        RealStation newRealStation = realStationService.creatRealStation(stationId, realStation);
        return new ResponseEntity<>(new ResultObject<>(newRealStation), HttpStatus.OK);
    }

    /**
     * 更新真实园区
     * @param realStation 电站对象
     */
    @ApiOperation("更新真实园区")
    @RequestMapping(value = "/updateRealStation", method = RequestMethod.POST)
    @OperationRecord(menu = "真实园区管理", actionType = SystemConsts.ACTION.EDIT)
    public ResponseEntity<ResultObject> post(@RequestBody RealStation realStation) throws ObjectNotFoundException, UniqueConstraintsException {
        RealStation newRealStation = realStationService.update(realStation);
        return new ResponseEntity<>(new ResultObject<>(newRealStation), HttpStatus.OK);
    }

    /**
     * 查询真实园区
     * @param stationId 总园区ID
     */
    @ApiOperation("查询真实园区")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "stationId", value = "总园区ID", required = true, dataType = "long", paramType = "path")
    })
    @RequestMapping(value = "/searchByStationId/{stationId}", method = RequestMethod.GET)
    public ResponseEntity<ResultObject> searchByStationId(@PathVariable("stationId") Long stationId) {
        Iterable<RealStation> realStationList = realStationService.findRealStationByStationId(stationId);
        return new ResponseEntity<>(new ResultObject<>(realStationList), HttpStatus.OK);
    }

    /**
     * 查询真实园区
     * @param id
     */
    @ApiOperation("通过id查询真实园区")
    @RequestMapping(value = "/searchById/{id}", method = RequestMethod.GET)
    public ResponseEntity<ResultObject> searchById(@PathVariable("id") Long id) {
        RealStation realStation = realStationService.searchById(id);
        return new ResponseEntity<>(new ResultObject<>(realStation), HttpStatus.OK);
    }

    /**
     * 根据名称查询真实园区
     * @param stationId 真实园区ID
     * @param title 园区名称
     */
    @ApiOperation("根据名称查询真实园区")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "stationId", value = "真实园区ID", required = true, dataType = "long", paramType = "path"),
            @ApiImplicitParam(name = "title", value = "园区名称", required = true, dataType = "String", paramType = "query")
    })
    @RequestMapping(value = "/findByTitle/{stationId}", method = RequestMethod.GET)
    public ResponseEntity<ResultObject> findByTitle(@PathVariable(value = "stationId") Long stationId,
                                                    @RequestParam(value = "title") String title) {
        RealStation realStation = realStationService.findByTitle(stationId, title);
        return new ResponseEntity<>(new ResultObject<>(realStation), HttpStatus.OK);
    }

    /**
     * 根据id查询真实园区信息
     * @param id 真实园区ID
     */
    @ApiOperation("根据id查询真实园区信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "真实园区ID", required = true, dataType = "long", paramType = "path")
    })
    @RequestMapping(value = "/findById/{id}", method = RequestMethod.GET)
    public ResponseEntity<ResultObject> findById(@PathVariable(value = "id") Long id) {
        RealStation realStation = realStationService.findById(id);
        return new ResponseEntity<>(new ResultObject<>(realStation), HttpStatus.OK);
    }

    /**
     * 查询真实园区（携带厂房）
     * @param stationId 总园区ID
     */
    @ApiOperation("查询真实园区（携带厂房）")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "stationId", value = "总园区ID", required = true, dataType = "long", paramType = "path")
    })
    @RequestMapping(value = "/searchStationAndOfficeByStationId/{stationId}", method = RequestMethod.GET)
    public ResponseEntity<ResultObject<Iterable<RealStation>>> searchStationAndOfficeByStationId(@PathVariable("stationId") Long stationId) {
        Iterable<RealStation> realStationList = realStationService.searchStationAndOfficeByStationId(stationId);
        return new ResponseEntity<>(new ResultObject<>(realStationList), HttpStatus.OK);
    }

    @ApiOperation("查询真实园区房源企业信息）")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "stationId", value = "总园区ID", required = true, dataType = "long", paramType = "path")
    })
    @RequestMapping(value = "/searchStationAndOfficeAndCompanyByStationId/{stationId}", method = RequestMethod.GET)
    public ResponseEntity<ResultObject<List<RealStationOfficeCompanyVo>>> searchStationAndOfficeAndCompanyByStationId(@PathVariable("stationId") Long stationId) {
        List<RealStationOfficeCompanyVo> realStationList = realStationService.searchStationAndOfficeAndCompanyByStationId(stationId);
        return new ResponseEntity<>(new ResultObject<>(realStationList), HttpStatus.OK);
    }

    @ApiOperation("查询真实园区房源企业信息")
    @RequestMapping(value = "/searchStationAndOfficeAndCompany", method = RequestMethod.POST)
    public ResponseEntity<ResultObject<List<RealStationOfficeCompanyVo>>> searchStationAndOfficeAndCompany(@RequestBody List<String> value) {
        List<RealStationOfficeCompanyVo> realStationList = realStationService.searchStationAndOfficeAndCompany(value);
        return new ResponseEntity<>(new ResultObject<>(realStationList), HttpStatus.OK);
    }

    /**
     * 查询真实园区（携带厂房）
     */
    @ApiOperation("查询真实园区")
    @RequestMapping(value = "/searchStationByStationId", method = RequestMethod.GET)
    public ResponseEntity<ResultObject> searchStationByStationId() {
        String projectCode = ServerContextHolder.getContext().getProjectCode();
        Iterable<RealStation> realStationList = realStationService.searchStationAndOfficeByStationId(Long.valueOf(projectCode));
        return new ResponseEntity<>(new ResultObject<>(realStationList), HttpStatus.OK);
    }

    @ApiOperation("查询真实园区")
    @RequestMapping(value = "/searchRealStationByStationId", method = RequestMethod.GET)
    public ResponseEntity<ResultObject> searchRealStationByStationId() {
        String projectCode = ServerContextHolder.getContext().getProjectCode();
        List<RealStationVo> realStationList = realStationService.searchRealStationByStationId(Long.valueOf(projectCode));
        return new ResponseEntity<>(new ResultObject<>(realStationList), HttpStatus.OK);
    }

    @ApiOperation("根据虚拟园区查询真实园区")
    @RequestMapping(value = "/searchRealStationByVirtualStationId", method = RequestMethod.GET)
    public ResponseEntity<ResultObject<List<RealStationVo>>> searchRealStationByStationId(@RequestParam("stationId") Long stationId) {
        List<RealStationVo> realStationList = realStationService.searchRealStationByStationId(stationId);
        return new ResponseEntity<>(new ResultObject<>(realStationList), HttpStatus.OK);
    }

    @ApiOperation("查询当前项目下真实园区以及已租或已售的房源信息")
    @RequestMapping(value = "/searchRealStationAndOffice", method = RequestMethod.GET)
    public ResponseEntity<ResultObject<RealStationOfficeVo>> searchRealStationAndOffice() {
        RealStationOfficeVo realStationOfficeVo = realStationService.searchRealStationAndOffice();
        return new ResponseEntity<>(new ResultObject<>(realStationOfficeVo), HttpStatus.OK);
    }

    /**
     * 删除真实园区
     * @param id 真实园区ID
     */
    @ApiOperation("删除真实园区")
    @RequestMapping(value = "/deleteRealStation/{id}", method = RequestMethod.DELETE)
    @OperationRecord(menu = "真实园区管理", actionType = SystemConsts.ACTION.DELETE)
    public ResponseEntity<ResultObject> deleteRealStation(@PathVariable(value = "id") Long id) {
        realStationService.deleteById(id);
        return new ResponseEntity<>(new ResultObject<>("success"), HttpStatus.OK);
    }
}
